import os
from pathlib import Path
import shutil
import re


file_dir="E:\周丹丽研究生\语言学python\语料"
pattern_blank=" "#文件夹的名字（其实是前后不能带）不带空格
for root,dirs,files in os.walk(file_dir):
	list_none=[]
	if files==list_none:
		continue
	
	p = Path(root)  # 初始化构造Path对象
	#print(root)
	file_object_list = list(p.glob("./*_out.txt"))
	for file in file_object_list:
		str_filename=file.name
		#print(str_filename)
		start_index=0
		for i in range(0,len(str_filename)):
			if str_filename[i]=='%':
				start_index=i
				break
		#print(start_index)
		str_age=""
		for i in range(start_index+1,len(str_filename)):
			if str_filename[i]=='%' or str_filename[i]=='.':
				break
			str_age=str_age+str_filename[i]
		if str_age=="":
			str_age='unknown'
		if str_age[0]==" " or str_age[len(str_age)-1]==" ":		
			str_age=re.sub(pattern_blank,"",str_age)
		#print(str_age)
		#print(root+'\\'+file.name)
		folder=os.path.exists('E:\周丹丽研究生\语言学python\语料分组\\'+str_age)
		
		if not folder:
			os.makedirs('E:\周丹丽研究生\语言学python\语料分组\\'+str_age)
			
			shutil.copy(root+'\\'+file.name,'E:\周丹丽研究生\语言学python\语料分组\\'+str_age)
		else:	
			#print(root+'\\'+file.name)
			shutil.copy(root+'\\'+file.name,'E:\周丹丽研究生\语言学python\语料分组\\'+str_age)

	file_object_list = list(p.glob("./*_chuli.txt"))
	for file in file_object_list:
		str_filename=file.name
		#print(str_filename)
		start_index=0
		for i in range(0,len(str_filename)):
			if str_filename[i]=='%':
				start_index=i
				break
		#print(start_index)
		str_age=""
		for i in range(start_index+1,len(str_filename)):
			if str_filename[i]=='%' or str_filename[i]=='.':
				break
			str_age=str_age+str_filename[i]
		if str_age=="":
			str_age='unknown'
		if str_age[0]==" " or str_age[len(str_age)-1]==" ":
			str_age=re.sub(pattern_blank,"",str_age)
		#print(str_age)
		#print(root+'\\'+file.name)
		folder=os.path.exists('E:\周丹丽研究生\语言学python\语料分组\\'+str_age)
		
		if not folder:
			os.makedirs('E:\周丹丽研究生\语言学python\语料分组\\'+str_age)	
			shutil.copy(root+'\\'+file.name,'E:\周丹丽研究生\语言学python\语料分组\\'+str_age)
		else:	
			#print(root+'\\'+file.name)
			shutil.copy(root+'\\'+file.name,'E:\周丹丽研究生\语言学python\语料分组\\'+str_age)
